import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class Sure_Bet_ex {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String test4 = "http://www.totesport.com/portal?action=GoCategory&category=Football&Sports-Betting-Football";
		String test5 = "http://www.skybet.com/football";
		//String test6 = "http://www.888sport.com/bet?action=go_football";
		//test6 currently indicates the Tuesday match thread page.  
		String test6 = "http://www.888sport.com/bet?action=go_coupon&coupon_id=175";
		
		Jsoup.parse(test4);
		Jsoup.parse(test5);
		Jsoup.parse(test6);
		
		Document Doc4 = null;
		Document Doc5 = null;
		Document Doc6 = null;
		
		try {
			Doc4 = Jsoup.connect(test4).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Doc5 = Jsoup.connect(test5).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Doc6 = Jsoup.connect(test6).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//tests for crawSite
		//crawlSite4(Doc4);
		//crawlSite5(Doc5);
		//crawlSite6(Doc6);
		
		//Tests for extractWinningOdds
		String Test41 = "http://www.totesport.com/portal?action=GoEvent&ev_id=1165732";
		extractWinningOdds4(Test41);
		String Test61 = "http://www.888sport.com/bet?action=go_event&ev_id=3656644";
		extractWinningOdds6(Test61);

	}

	private static void crawlSite4(Document doc) {
		Elements e = doc.select("table > tbody > tr ");
		int i = 0;
		// TODO: Fix while true thing
		while(i < e.size()){
			Element MasList = e.get(i);
			//System.out.println(MasList);
			
			String URL = MasList.select("td.markets > ul > li > ul > li > div > dl > dd > a[href]").attr("abs:href");
			System.out.println(URL);
			//Get Odds of URL into the Object that Alex Makes
			extractWinningOdds4(URL);
			
			i++;
		
		}			
	}
	
	private static void crawlSite5(Document doc){
		Elements e = doc.select("div.market-wdw > table > tbody ");
		
		//note the 2 comes from the web site as we always want to look at the 3rd table on the page
		Element MasList1 = e.get(2);
		Elements Game = MasList1.select("tr");
		//System.out.println(MasList1);
		//System.out.println(Game);
		
		int count = 0;
		// TODO: Fix while true statement
		while(count < Game.size()){
		
			Element GameID = Game.get(count);
			//System.out.println(GameID);
			
			String URL = GameID.select(" td.all-bets-cell > a[href]").attr("abs:href");
			//System.out.println(URL);
			
			//This part parses the website and then calls (extracts the odds from the website)
			Jsoup.parse(URL);
			Document InDoc5 = null;
			
			try {
				InDoc5 = Jsoup.connect(URL).get();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//extractWinningOdds3(InDoc5);
			System.out.println(count);
			count++;
			
			
		}			
	}	
	
	private static void crawlSite6(Document doc) {
		Elements e = doc.select("table");
		Element f = e.get(3);
		Elements Odds = f.select("tr");
		//System.out.println(Odds);
		
		int i = 0;
		while(i+2 < Odds.size()){
			Element MasList = Odds.get(i+2);
			//System.out.println(MasList);
			
			//Check to see if a given game has a "more odds" page
			if(MasList.select("td > button").isEmpty()){
				//Skip to the next odd
				i++;
			}
			else{
				//Perform the rest of the task
				Elements lines = MasList.select("td > button");
				//System.out.println(lines);
				
				String URL = lines.attr("onclick").substring(17, lines.attr("onclick").length()-1);
				System.out.println(URL);	
				
				//extractWinningOdds6(InDoc6);
				i++;
			}
		}			
	}
	
	//NOTE: Half time full time is not always before correct score.
	//NOTE: Some games have far more odds than other games 
	private static void extractWinningOdds4(String URL) {		
	
		//This part parses the website and then calls (extracts the odds from the website)
		Jsoup.parse(URL);
		Document InDoc4 = null;
		
		try {
			InDoc4 = Jsoup.connect(URL).get();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Elements e = InDoc4.select("div.market_wrapper");
		//System.out.println(e);
		
		int count = 0;
		while(count < e.size()){
			String Bet_Type = e.get(count).select("div.market_title > h2").text();
			//System.out.println(Bet_Type);
			
			//This gets the odds of the main game 
			if(Bet_Type.equals("90 Minutes")
					|| Bet_Type.equals("Second Half Result")
					){
				Elements Game1 = e.get(count).select("tbody > tr > td");
				//System.out.println(Game1);
				System.out.println("===============");
				System.out.println(Bet_Type);
				System.out.println("===============");
				int count1 = 0;
				while(count1 < Game1.size()){
					String Bet_Name = Game1.get(count1).text();
					System.out.println(Bet_Name);		
					count1++;
				}
			}
			
			//add everything but second half result to this one
			
			//This gets the odds of the Half Time / Full time Results
			else if(Bet_Type.equals("Half-time/Full-time")
					|| Bet_Type.equals("Over / Under")
					|| Bet_Type.equals("Total Goals")
					|| Bet_Type.equals("Total Goals 2")
					|| Bet_Type.equals("Time Of 1st Goale")
					|| Bet_Type.equals("Half Time Result")
					|| Bet_Type.equals("HT Total Goals")
					|| Bet_Type.equals("HT Totals Over/Under")
					|| Bet_Type.equals("To Score First Goal")
					|| Bet_Type.equals("To Score Last Goal")
					|| Bet_Type.equals("Handicap (-1)")
					|| Bet_Type.equals("Handicap (+1)")
					|| Bet_Type.equals("Half Most Goals")
					|| Bet_Type.equals("Odd/Even")
					|| Bet_Type.equals("2nd Half Total Goals")
					|| Bet_Type.equals("Goals 10-band")
					){
				Elements Game2 = e.get(count).select("tbody > tr > td");
				//System.out.println(Game2);
				int count2=0;
				System.out.println("===============");
				System.out.println(Bet_Type);
				System.out.println("===============");
				while (count2 < Game2.size()){
						String Name = Game2.get(count2).text();
						String Odds = Game2.get(count2 + 1).text();
						System.out.println("Name: " +  Name + " Odds: "+ Odds);
						count2++;
						count2++;
				}
			}
			count++;
		}
	}

	private static void extractWinningOdds6(String URL) {
		Jsoup.parse(URL);
		Document InDoc6 = null;
		
		try {
			InDoc6 = Jsoup.connect(URL).get();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//
		Elements e = InDoc6.select("table > tbody > tr > td > form ");
		//System.out.println(e.get(0));
		
		//this will get the table with the bets out 
		//Elements Game1 = e.select("table.darkLightDarkText");
		Elements Game1 = e.select("table");
		
		int count = 0;
		//System.out.println(Game1.get(5));
		String Bet_Type = null;
		while(count < Game1.size()){
			//maybe an if statement based on class
			//if class is bgGlassBar it has the title
			//if class is darkLightDarkText it has odds
			//everything else should be useless
			
			if(Game1.get(count).attr("class").equals("bgGlassBar")){
				Bet_Type = Game1.get(count).select("tbody > tr > td").text();
				//System.out.println(Bet_Type);
			}
			else if (Game1.get(count).attr("class").equals("darkLightDarkText")){
				//System.out.println(Bet_Type);
				if(Bet_Type.equals("Win/Draw/Win ")
						|| Bet_Type.equals("Draw No Bet ")
						|| Bet_Type.equals("Total Goals ")
						|| Bet_Type.equals("Total Goals Over/Under ")
						|| Bet_Type.equals("Handicap ")
						|| Bet_Type.equals("Double Chance ")
						|| Bet_Type.equals("Half-Time/Full-Time ")
						|| Bet_Type.equals("Half-Time Result ")
						|| Bet_Type.equals("Team to Score First ")
						|| Bet_Type.equals("Time of First Goal ")
						|| Bet_Type.equals("Total Goal Minutes ")
						|| Bet_Type.equals("Half With Most Goals ")
						|| Bet_Type.equals("Time of First Goal (Brackets) ")
						|| Bet_Type.equals("Teams to Score ")
						|| Bet_Type.equals("Team Goals ")
						){
					System.out.println("==============");
					System.out.println(Bet_Type);
					System.out.println("==============");
					
					//each td in the odds 1 has a name and a odd
					Elements Odds1 = Game1.get(count).select("tbody > tr");
					//System.out.println(Odds1);
					int i = 0;
					while(i < Odds1.size()){
						String Name = Odds1.get(i).select("td").get(0).text();
						String Odd = Odds1.get(i).select("td").get(1).text();
						System.out.println("Name: "+ Name + " Odds: "+ Odd);
						i++;
					}
				}
			}
			count++;
		}
	}
}
